//
//  HomeVedioPlayerControllerViewController.m
//  QSBK
//
//  Created by Eric Wang on 4/22/15.
//  Copyright (c) 2015 Eric Wang. All rights reserved.
//

#import "HomeVedioPlayerController.h"

@interface HomeVedioPlayerController ()
{
    NSMutableArray *_constraint;
}
@property(nonatomic,strong)UIImageView *progressImageView;
@property(nonatomic,strong)MPMoviePlayerController *player;
@end

@implementation HomeVedioPlayerController



- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    
    self.view.backgroundColor = [UIColor clearColor];
    self.bgImageView = [[UIImageView alloc]init];
    self.bgImageView.image = self.imageData;
    self.bgImageView.translatesAutoresizingMaskIntoConstraints = NO;
    
    _constraint = [[NSMutableArray alloc]init];
    self.progressImageView = [[UIImageView alloc]init];
    self.progressImageView.translatesAutoresizingMaskIntoConstraints = NO;
    self.progressImageView.image = [UIImage imageNamed:@"noarrow.png"];
    [self.view addSubview:self.bgImageView];
    [self.view addSubview:self.progressImageView];
    
    
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[view]-(0)-|" options:0 metrics:nil views:@{@"view":self.bgImageView}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[view]-(0)-|" options:0 metrics:nil views:@{@"view":self.bgImageView}]];
    
    
    NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:self.progressImageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
    NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:self.progressImageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];
    
    NSLayoutConstraint *constraint3 = [NSLayoutConstraint constraintWithItem:self.progressImageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:60];
    
    NSLayoutConstraint *constraint4 = [NSLayoutConstraint constraintWithItem:self.progressImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:60];
    
    [_constraint addObject:constraint1];
    [_constraint addObject:constraint2];
    [_constraint addObject:constraint3];
    [_constraint addObject:constraint4];
    
    [self.view addConstraints:_constraint];
    
    
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 1.5;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = MAXFLOAT;
    [self.progressImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    
    
    
    
    
    self.moviePlayer.controlStyle = MPMovieControlStyleNone;
    //[self.moviePlayer setAllowsAirPlay:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieStart) name:MPMoviePlayerReadyForDisplayDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieStop) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    // Do any additional setup after loading the view.
}



-(void)movieStart
{
    
    [self.progressImageView.layer removeAllAnimations];
    self.progressImageView.hidden = YES;
    self.bgImageView.hidden = YES;
}

-(void)movieStop
{
    dispatch_async(dispatch_get_main_queue(),^{
       [self.view removeFromSuperview];
        if(self.delegate!=nil&&[self.delegate respondsToSelector:@selector(movieDidStop)])
        {
            [self.delegate movieDidStop];
        }
    });
    
    
}

-(void)setLow_url:(NSURL *)low_url
{
    _low_url = low_url;
    self.bgImageView.image = self.imageData;
}
//-(void)
@end
